//
//  LoginStore.m
//  NewPro
//
//  Created by 何荧红 on 2019/4/25.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "LoginStore.h"

@implementation LoginStore
#pragma mark 登陆
- (void)PostWithCustomName:(NSString *)customName passWord:(NSString *)passWord Success:(void(^)(UserDefaultsManager *model))success Failure:(void(^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/api/login",IP];
    NSDictionary *prams = @{
                          @"customName":customName,
                          @"passWord":passWord
                          };
    
    [HttpTool postUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@", responseObject[@"data"][@"info"][@"id"]] forKey:@"id"];
            [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"custom"][@"token"] forKey:@"token"];
            [[NSUserDefaults standardUserDefaults] setObject:passWord forKey:@"pwd"];
            [[NSUserDefaults standardUserDefaults] setObject:customName forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"info"][@"realName"] forKey:@"realname"];
            NSString *headimg = responseObject[@"data"][@"info"][@"headImage"];
            if ([headimg isEqual:[NSNull null]]) {
                [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"headImage"];
            }else{
                [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"info"][@"headImage"] forKey:@"headImage"];
            }
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            UserDefaultsManager *manager = [UserDefaultsManager sharedInstance];
            manager.user_avatar = responseObject[@"data"][@"info"][@"headImage"];
            manager.user_id = [NSString stringWithFormat:@"%@", responseObject[@"data"][@"info"][@"id"]];
            manager.user_pwd = passWord;
            manager.user_name = customName;
            manager.real_name = responseObject[@"data"][@"info"][@"realName"];
            success(manager);
        }else{
            failure(error);
        }
    } failure:^(NSError *error) {
        NSLog(@"报错了======%@",[[NSString alloc] initWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
        failure(error);
    }];
}

#pragma mark 刷新友盟token
- (void)updateWithDeviceToken{
    NSString *url = [NSString stringWithFormat:@"%@/api/push/token",IP];
    NSDictionary *prams = @{
                          @"customId":[UserDefaultsManager sharedInstance].user_id,
                          @"token":[UserDefaultsManager sharedInstance].deviceToken
                          };
    [HttpTool postUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            
        }else{
            
        }
    } failure:^(NSError *error) {
        NSLog(@"报错了======");
    }];
}
#pragma mark 注册
- (void)RegisterWithPrams:(NSDictionary *)prams Success:(void(^)(NSString *code))success Failure:(void(^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/api/register",IP];
    [HttpTool postUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            if ([responseObject[@"msg"] isEqualToString:@"success"]) {
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",responseObject[@"data"][@"customId"]] forKey:@"id"];
                [[NSUserDefaults standardUserDefaults] setObject:prams[@"userName"] forKey:@"username"];
                [[NSUserDefaults standardUserDefaults] setObject:prams[@"realName"] forKey:@"realname"];
                [[NSUserDefaults standardUserDefaults] setObject:prams[@"passWord"] forKey:@"pwd"];
                UserDefaultsManager *manager = [UserDefaultsManager sharedInstance];
                manager.user_id = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"customId"]];
                manager.user_pwd = prams[@"passWord"];
                manager.user_name = prams[@"userName"];
                manager.real_name = prams[@"realName"];
            }
            success(responseObject[@"msg"]);
        }else{
            failure(error);
        }
    } failure:^(NSError *error) {
        NSLog(@"报错了======%@",[[NSString alloc] initWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
        failure(error);
    }];
}
#pragma mark 注册获取验证码
- (void)sendCodeWithTelephoneNumber:(NSString *)telephoneNumber Success:(void(^)(id res))success Failure:(void(^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/api/getRegMsgCode",IP];
    NSDictionary *prams = @{
                            @"telephoneNumber":telephoneNumber
                            };
    [HttpTool postUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            success(responseObject);
        }else{
           failure(error);
        }
    } failure:^(NSError *error) {
        NSLog(@"报错了======%@",[[NSString alloc] initWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
        failure(error);
    }];
}
#pragma mark 忘记密码验证码
- (void)ForgetsendCodeWithTelephoneNumber:(NSString *)telephoneNumber Success:(void(^)(id res))success Failure:(void(^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/api/getForgetPasswordMsgCode",IP];
    NSDictionary *prams = @{
                            @"telephoneNumber":telephoneNumber
                            };
    [HttpTool postUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            success(responseObject);
        }else{
            failure(error);
        }
    } failure:^(NSError *error) {
        NSLog(@"报错了======%@",[[NSString alloc] initWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
        failure(error);
    }];
}
#pragma mark 忘记密码时修改密码
- (void)forgetPassword:(NSDictionary *)prams Success:(void(^)(NSString *code))success Failure:(void(^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/api/forgetPassdUpdatePassword",IP];
    [HttpTool postUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            success(responseObject[@"msg"]);
        }else{
            failure(error);
        }
    } failure:^(NSError *error) {
        NSLog(@"报错了======%@",[[NSString alloc] initWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
        failure(error);
    }];
}












@end
